var app = getApp();
var request = app.request;
var setting = app.globalData.setting;
import Regions from '../../../utils/regions/Regions.js';
Page({
	data: {
		url: setting.url,
		store: {},
		address: {}, //收货地址信息
	},

	onLoad: function() {
		this.requestMyStore();
		this.initRegions()
	},
	// 删除当前图片
	deleteImg(e) {
		let list = [...this.data.store.shop_images]
		list.splice(e.currentTarget.dataset.index, 1)
		this.setData({
			'store.shop_images': list
		});
	},
	// 开门时间
	bindTimeChange(e) {
		this.setData({
			['store.work_end_time']: e.detail.value
		})
	},
	// 开门时间
	bindTimeChange1(e) {
		this.setData({
			['store.work_start_time']: e.detail.value
		})
	},
	/** 初始化区域弹框相关 */
	initRegions: function() {
		var that = this;
		new Regions(this, 'regions', {
			endAreaLevel: 3,
			endAreaLevelCall: function(parentId, regionName, address) {
				Object.assign(that.data.address, address);
				that.setData({
					'address.province_name': that.data.address.province_name,
					'address.city_name': that.data.address.city_name,
					'address.district_name': that.data.address.district_name,
					'store.province_id': that.data.address.province,
					'store.city_id': that.data.address.city,
					'store.district_id': that.data.address.district
				});
			}
		});
	},
	// 获取当前经纬度
	lalo() {
		let that = this
		wx.getLocation({
			isHighAccuracy: true,
			success(res) {
				that.setData({
					'store.longitude': res.longitude,
					'store.latitude': res.latitude,
				});
			}
		})
	},
	requestMyStore: function() {
		var that = this;
		// ('/api/distribut/store'
		request.get('/api/shop/getShopInfo', {
			success: function(res) {
				let imglist = []
				res.data.result.shop_images.forEach(e => {
					imglist.push(e.image_url)
				})

				let awlist = {
					province_name: res.data.result.region_address.split(' ')[0],
					city_name: res.data.result.region_address.split(' ')[1],
					district_name: res.data.result.region_address.split(' ')[2]
				}
				res.data.result.shop_images = imglist
				that.setData({
					store: res.data.result,
					address: awlist
				});
			},
			failStatus: function() {
				return false;
			}
		});
	},

	submitStore: function(e) {
		var that = this;
		var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
		var data = Object.assign(e.detail.value, {
			province_id: this.data.store.province_id,
			city_id: this.data.store.city_id,
			shop_pic: this.data.store.shop_pic,
			district_id: this.data.store.district_id,
			shop_images: this.data.store.shop_images,
			work_start_time: this.data.store.work_start_time,
			work_end_time: this.data.store.work_end_time
		});
		if (data.shop_name.replace(/\s+/g, '') == "") {
			return app.showTextWarining("请输入自提点名称");
		} else if (data.shop_address.replace(/\s+/g, '') == "") {
			return app.showTextWarining("请输入自提点地址");
		} else if (data.province_id == 0 || data.province_id == '') {
			return app.showTextWarining("请选择地址");
		}
		request.post('/api/shop/editInfo', {
			data: data,
			isShowLoading: false,
			success: function(res) {
				if (res.data.status == 1) {
					app.showSuccess(res.data.msg);
					setTimeout(function() {
						that.requestMyStore();
					}, 2000) // 

				}

			}
		});
		// request.post('/api/distribut/store', {
		//     data: data,
		//     success: function (res) {
		//         app.showSuccess(res.data.msg);
		//         that.setData({ store: res.data.result });
		//         wx.navigateBack();
		//     }
		// });
	},

	changeStoreImg: function(e) {
		var that = this;
		wx.chooseImage({
			count: 1, //最多1张图片,默认9
			sizeType: ['compressed', 'original'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['camera', 'album'], // 可以指定来源是相册还是相机，默认二者都有
			success: function(res) {
				request.uploadFile('/api/distribut/upload_store_img', {
					filePath: res.tempFilePaths[0],
					name: 'store_img',
					success: function(res) {
						let list = [...that.data.store.shop_images]
						if (e.currentTarget.dataset.index) {
							list[e.currentTarget.dataset.index] = res.data.result
						} else {
							list.push(res.data.result)
						}
						that.setData({
							'store.shop_images': list
						});
					}
				});
			}
		});
	},
	changeStorePic: function() {
		var that = this;
		wx.chooseImage({
			count: 1, //最多1张图片,默认9
			sizeType: ['compressed', 'original'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['camera', 'album'], // 可以指定来源是相册还是相机，默认二者都有
			success: function(res) {
				request.uploadFile('/api/distribut/upload_store_pic', {
					filePath: res.tempFilePaths[0],
					name: 'store_pic',
					success: function(res) {
						that.setData({
							'store.shop_pic': res.data.result
						});
					}
				});
			}
		});
	}
})